//-----------------------------------------------------------------------
// <copyright file="LoaiDanhMucDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using APPro.Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace APPro.Data
{    
    public abstract class LoaiDanhMucDAOBase
    {
        #region Common methods
        public virtual LoaiDanhMuc CreateLoaiDanhMucFromReader(IDataReader reader)
        {
            LoaiDanhMuc item = new LoaiDanhMuc();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("MaLoaiDanhMuc"))) item.MaLoaiDanhMuc = (int)reader["MaLoaiDanhMuc"];
                if (!reader.IsDBNull(reader.GetOrdinal("TenLoaiDanhMuc"))) item.TenLoaiDanhMuc = (string)reader["TenLoaiDanhMuc"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateLoaiDanhMucFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreateLoaiDanhMuc methods
            
        public virtual void CreateLoaiDanhMuc(LoaiDanhMuc loaiDanhMuc)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spLoaiDanhMucInsert");
                
				database.AddInParameter(dbCommand, "@MaLoaiDanhMuc", DbType.Int32, loaiDanhMuc.MaLoaiDanhMuc);
				database.AddInParameter(dbCommand, "@TenLoaiDanhMuc", DbType.String, loaiDanhMuc.TenLoaiDanhMuc);
				
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateLoaiDanhMucException, ex);
            }
        }

        #endregion
		
        #region UpdateLoaiDanhMuc methods
        
        public virtual void UpdateLoaiDanhMuc(LoaiDanhMuc loaiDanhMuc)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spLoaiDanhMucUpdate");            
                
                database.AddInParameter(dbCommand, "@MaLoaiDanhMuc", DbType.Int32, loaiDanhMuc.MaLoaiDanhMuc);
                database.AddInParameter(dbCommand, "@TenLoaiDanhMuc", DbType.String, loaiDanhMuc.TenLoaiDanhMuc);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdateLoaiDanhMucException, ex);
            }
        }
        
        #endregion
		
        #region DeleteLoaiDanhMuc methods
        public virtual void DeleteLoaiDanhMuc(int maLoaiDanhMuc)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spLoaiDanhMucDelete");
                
                database.AddInParameter(dbCommand, "@MaLoaiDanhMuc", DbType.Int32, maLoaiDanhMuc);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeleteLoaiDanhMucException, ex);
            }
        }

        #endregion
		
        #region GetLoaiDanhMuc methods
        
        public virtual LoaiDanhMuc GetLoaiDanhMuc(int maLoaiDanhMuc)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spLoaiDanhMucGet");
                
                database.AddInParameter(dbCommand, "@MaLoaiDanhMuc", DbType.Int32, maLoaiDanhMuc);
                
                LoaiDanhMuc loaiDanhMuc =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        loaiDanhMuc = CreateLoaiDanhMucFromReader(reader);
                        reader.Close();
                    }
                }
                return loaiDanhMuc;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetLoaiDanhMucException, ex);
            }
        }
        
        #endregion
		
        #region GetLoaiDanhMucList methods
        public virtual LoaiDanhMucCollection GetLoaiDanhMucList(LoaiDanhMucColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spLoaiDanhMucGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                LoaiDanhMucCollection loaiDanhMucCollection = new LoaiDanhMucCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        LoaiDanhMuc loaiDanhMuc = CreateLoaiDanhMucFromReader(reader);
                        loaiDanhMucCollection.Add(loaiDanhMuc);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return loaiDanhMucCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetLoaiDanhMucListException, ex);
            }
        }
        
        public virtual LoaiDanhMucCollection GetLoaiDanhMucList(LoaiDanhMucColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetLoaiDanhMucList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
    }
}